<?php
require_once 'include.php';
check_login();#检查是否登录
$total = sql_count('lyy_gonggao','del=1');#总行数
$curr = isset($_REQUEST['curr'])?$_REQUEST['curr']:1;#当前页
$limit = 10;#每页多少行
$page = ceil($total/$limit);#总共有多少页
$offset = ($curr-1)*$limit;
$sql = "select a.id,a.content,a.status,a.updatetime,b.username from lyy_gonggao a 
JOIN lyy_member b ON a.uid=b.id WHERE a.del=1 ORDER BY a.createtime limit $offset,$limit";
$res = mysqli_query(conn(),$sql);
$lists = mysqli_fetch_all($res,MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <?php include_once 'common_js_css.php'?>
    <link rel="stylesheet" type="text/css" href="css/adgonggao.css">
</head>
<body>
<div class="cont" style="height: 730px">
    <h1 style="text-align: center;">公告列表</h1>
    <form id="form1" style="margin-left: 40px;margin-top: 40px">
      <table width="980" border="0" align="center" cellspacing="0">
        <tr>
          <th width="45" height="30" scope="col">序号</th>
          <th width="800" height="30" scope="col">内容</th>
          <th width="100" height="30" scope="col">发布者</th>
          <th width="200" height="30" scope="col">更新时间</th>
          <th width="100" height="30" scope="col">发布状态</th>
          <th width="200" height="30" scope="col">操作</th>
        </tr>
          <?php foreach ($lists as $list): ?>
        <tr>
          <td height="30" align="center"><?php echo $list['id'];?></td>
          <td height="50" align="center"><?php echo $list['content'];?></td>
          <td height="30" align="center"><?php echo $list['username'];?></td>
          <td height="30" align="center"><?php echo date("Y-m-d H:i:s",$list['updatetime']);?></td>
          <td height="30" align="center"><?php if ($list['status']==1) echo '公告中'; else {echo '隐藏中';} ?></td>
          <td height="30" align="center">
              <input type="submit" onclick="del('lyy_gonggao','<?php echo $list['id'];?>')" value="删除">
              <input class="container" type="button" onclick="xiugai_gonggao('<?php echo $list['id']; ?>','<?php echo $list['status']; ?>')" value="修改">
          </td>
        </tr>
          <?php endforeach; ?>
          <tr>
              <td colspan="6" align="center">
                  <!--显示分页，只有一页，即不显示分页-->
                  <?php if($total>$limit){echo showPage($curr,$page);}?>
                  <input name="Submit2" class="print" type="button" onClick="javascript:window.print();" value="打印本页" />
              </td>
          </tr>
      </table>
    </form>
</div>

<!--修改弹窗-->
<div class="fulled">
    <div class="fix-box">
        <p class="close">关闭</p>
        <span style="font: normal normal 35px '楷体';">修改</span>
        <table align="center" cellspacing="0">
            <tr>
                <td align="center" height="40">发布状态</td>
                <td>
                    <select id="status">
                        <option value="1">发布</option>
                        <option value="2">隐藏</option>
                    </select>
                    <input type="text" id="id" style="display: none">
                </td>
            </tr>
            <tr>
                <td align="center">公告内容</td>
                <td height="30"><textarea name="cont" rows="6" id="content"></textarea></td>
            </tr>
            <tr>
                <td colspan="4" align="center" height="50">
                    <input type="button" value="修改" onclick="update_gonggao()">
                </td>
            </tr>
        </table>
    </div>
</div>

<script>
    function xiugai_gonggao(id,type,content) {
        $('#id').val(id);
        $('#status').val(type);
        $('#content').val(content);
    }
    function update_gonggao() {
        var content = $("#content").val(),
            status = $("#status").val(),
            id = $("#id").val();
        if(content=="" || content==null){
            $("#titles").empty();
            $("#content").after("<span id='titles' style='color: red'>内容不能为空</span>");
            return false;
        }
        else {
            post_data={
                content:content,
                status:status,
                id:id
            };
            $.post('doAction.php?act=update_gonggao',post_data,function (data) {
                if (data==200){
                    alert('修改成功');
                    window.location = 'adlist_gonggao.php';
                }else {
                    alert(data);
                }
            })
        }
    }
</script>
</body>
</html>